/*
 * gpio.c
 *
 *  Created on: 14/mar/2014
 *      Author: Mario
 */

#include "gpio.h"

/**
 * @brief  configures specified GPIO pin as output.
 * @param  GPIOx: where x can be (A..I) to select the GPIO peripheral.
 * @param  GPIO_Pin: specifies the port bit to be configured in output mode.
 *          This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
 * @param  GPIO_Mode: Specify GPIO Configuration i.e. input/output/ADC/AF
 *         This parameter can be a value of @ref GPIOMode_TypeDef
 * @retval None
 */
void InitGPIO(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin,
		GPIOMode_TypeDef GPIO_Mode, GPIOPuPd_TypeDef GPIO_PuPdMode, GPIOSpeed_TypeDef GPIO_Speed ) {
	GPIO_InitTypeDef GPIO_InitStructure;

	//Enables the clock of GPIO port
	//Cannot use the switch-case due to a not integer quantity
	if (GPIOx == GPIOA) {
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	} else if (GPIOx == GPIOB) {
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	} else if (GPIOx == GPIOC) {
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	} else if (GPIOx == GPIOD) {
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	} else if (GPIOx == GPIOE) {
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	} else if (GPIOx == GPIOF) {
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	} else
		return;

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //used for digital output and PWM output
	//this setting does not matter for ADC and digital read
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPdMode;
	GPIO_Init(GPIOx, &GPIO_InitStructure);
}
